﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuDada.Core.Post;
using WuDada.Core.Post.Service;
using WuDada.Core.Post.Domain;
using System.Text;

public partial class Index : System.Web.UI.Page
{
    private PostFactory m_PostFactory;
    private IPostService m_PostService;

    protected void Page_Load(object sender, EventArgs e)
    {
        m_PostFactory = new PostFactory();
        m_PostService = m_PostFactory.GetPostService();

        if (!IsPostBack)
        {
            FillService();
            FillNews();
            FillWitness();
            FillAdv();
        }
    }

    private void FillAdv()
    {
        //搜尋條件
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", "17");
        conditions.Add("Flag", "1");

        RepeaterAdv.DataSource = m_PostService.GetPostList(conditions);
        RepeaterAdv.DataBind();
    }

    private void FillWitness()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("ParentNodeId", "8");
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", "0");
        conditions.Add("PageSize", "3");
        RepeaterWitness.DataSource = m_PostService.GetPostList(conditions);
        RepeaterWitness.DataBind();
    }

    private void FillService()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        IList<NodeVO> list = m_PostService.GetNodeListByParentId(7);
        StringBuilder sb = new StringBuilder();

        if (list != null && list.Count > 0)
        {
            foreach (NodeVO nodeVO in list)
            {
                sb.AppendFormat("<li class='leftLIst-li'><a href='service.aspx?node={0}' class='whitebtn'>{1}</a></li>", nodeVO.NodeId, nodeVO.Name);
            }

            ltlService.Text = sb.ToString();
        }
    }

    private void FillNews()
    {
        Dictionary<string, string> conditions = new Dictionary<string, string>();
        conditions.Add("NodeId", "4");
        conditions.Add("Flag", "1");
        conditions.Add("PageIndex", "0");
        conditions.Add("PageSize", "3");
        IList<PostVO> list = m_PostService.GetPostList(conditions);
        StringBuilder sb = new StringBuilder();

        if (list != null && list.Count > 0)
        {
            foreach (PostVO postVO in list)
            {
                string title = postVO.Title;
                if (title.Length > 22)
                {
                    title = title.Substring(0, 22) + "...";
                }

                sb.AppendFormat("<li class='Left-News-LI'><a href='news_in.aspx?id={0}' class='whitebtn'>{1}</a></li>", postVO.PostId, title);
            }

            ltlNews.Text = sb.ToString();
        }
    }

    protected void RepeaterWitness_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemIndex != -1)
        {
            Control ctrl = e.Item;
            PostVO postVO = (PostVO)e.Item.DataItem;
            postVO = m_PostService.GetPostByIdNoLazy(postVO.PostId);

            //標題
            int customField1Length = 38;
            if (!string.IsNullOrEmpty(postVO.CustomField1))
            {
                string title = postVO.CustomField1;
                if (title.Length > customField1Length)
                {
                    title = title.Substring(0, customField1Length) + "...";
                }

                UIHelper.SetLiteralText(ctrl, "ltlCustomField1", title);
            }

            UIHelper.SetLiteralText(ctrl, "ltlNodeName", postVO.Node.Name);
        }
    }
}